package com.zodiac.app.datathinking.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@Component
public class MailService {
    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    public void simpleSend() {
        final SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("Test");
        message.setText("Hello World");
        message.setTo("zhangguangyong@yqcreditrisk.com");
        message.setFrom(from);
        mailSender.send(message);
    }

    public void mimeSend(String html, File attachment) throws MessagingException {
        final MimeMessage message = mailSender.createMimeMessage();

        final MimeMessageHelper helper = new MimeMessageHelper(message, true);
        helper.setSubject("TestMime");
        helper.setText(html, true);
        helper.setTo("zhangguangyong@yqcreditrisk.com");
        helper.addInline("image001", attachment);
//        helper.addAttachment(attachment.getName(), attachment);
        message.setFrom(from);

        mailSender.send(message);
    }
}
